package virtualstock.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

import javax.ejb.EJBException;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "game", schema = "vstock")
@NamedQueries({
	@NamedQuery(name="Game.FindAllAvailableGame", query = "SELECT a FROM Game a"),
	@NamedQuery(name="Game.FindCreatedGameID", query = "SELECT a.gameID FROM Game a " +
			"WHERE a.gameName = :gameName AND a.creator = :creator AND a.maxPlayer = :maxPlayer AND a.initialBalance = :initialBalance AND a.startDate = :startDate")
})
public class Game implements Serializable {
	@Id
	@Column(name="gameID", nullable=false)
	private Long gameID;

	@Column(name="game_name")
	private String gameName;

	@Column(name="creator")
	private Long creator;

	@Column(name="max_player")
	private int maxPlayer;

	@Column(name="initial_balance")
	private BigDecimal initialBalance;

	@Column(name="start_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date startDate;
	
	@JoinTable(name = "portfolio", schema = "vstock", joinColumns = @JoinColumn(name = "gameID", referencedColumnName = "gameID")
		    , inverseJoinColumns = @JoinColumn(name = "playerID", referencedColumnName = "playerID")
		    )
	@ManyToMany
	private Collection<Player> players;

	private static final long serialVersionUID = 1L;

	public Game() {
		super();
	}
	
	public Game(
			String gameName,
			Long creator,
			int maxPlayer,
			BigDecimal initialBalance,
			Date date) {
		this.gameName = gameName;
		this.creator = creator;
		this.maxPlayer = maxPlayer;
		this.initialBalance = initialBalance;
		this.startDate = date;
	}

	public Long getGameID() {
		return this.gameID;
	}

	public void setGameID(Long gameID) {
		this.gameID = gameID;
	}

	public String getGameName() {
		return this.gameName;
	}

	public void setGameName(String gameName) {
		this.gameName = gameName;
	}

	public Long getCreator() {
		return this.creator;
	}

	public void setCreator(Long creator) {
		this.creator = creator;
	}

	public int getMaxPlayer() {
		return this.maxPlayer;
	}

	public void setMaxPlayer(int maxPlayer) {
		this.maxPlayer = maxPlayer;
	}

	public BigDecimal getInitialBalance() {
		return this.initialBalance;
	}

	public void setInitialBalance(BigDecimal initialBalance) {
		this.initialBalance = initialBalance;
	}

	public Date getStartDate() {
		return this.startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public void setPlayers(Collection<Player> players) {
		this.players = players;
	}

	public Collection<Player> getPlayers() {
		return players;
	}
	
	public void addPlayer(Player player) {
		try {
			this.players.add(player);
		} catch (Exception ex) {
			throw new EJBException(ex);
		}
	}
	
	public void removePlayer(Player player) {
		try {
			this.players.remove(player);
		} catch (Exception ex) {
			throw new EJBException(ex);
		}
	}

}
